#!/usr/bin/python

from euler.core import infinity
import cProfile
from euler.numbers.decimal_base import integer_to_digits

def get_answer():
	"""Question:
	
	The cube, 41063625 (3453), can be permuted to produce two other 
	cubes: 56623104 (3843) and 66430125 (4053). In fact, 41063625 is the 
	smallest cube which has exactly three permutations of its digits which are 
	also cube.

	Find the smallest cube for which exactly five permutations of its digits 
	are cube.
	"""
	
	def get_digit_occurences(n):
		"""Return the number of occurences for each digit in [n], along with
		0 for those digits which do not occur. Return this result in the form
		of a tuple, which contains tuple pairs of each digit and how many times
		it occurs in [n].
		
		Example:
			n = 12341234:  ((0, 0), (1, 2), (2, 2), (3, 2), (4, 2), 
							(5, 0), (6, 0), (7, 0), (8, 0), (9, 0))
								
			n = 55544422:  ((0, 0), (1, 0), (2, 2), (3, 0), (4, 3), 
							(5, 3), (6, 0), (7, 0), (8, 0), (9, 0))
		"""			
			
		perm = [0 for x in range(10)]
		digits = integer_to_digits(n)
		
		for digit in digits:
			perm[int(digit)] += 1
	
		result = []
		
		for i in range(10):
			result.append((i, perm[i]))
			
		return tuple(result)

	#Dictionary which maps a combination of digit occurences (1 occurs 3 times,
	#4 occurs 10 times, etc...) to the first cube with this pattern and the 
	#number of cubes which have this pattern.
	digit_occurences_dict = {}
	
	#For each cube that exists, check if its digit occurence pattern has 
	#already been recorded. If it has, increase the number of instances of this 
	#pattern. If not, record the pattern and add the cube as its first instance. 
	#If a digit occurence pattern with 5 instances is found, print the first 
	#cube with this pattern.
	for i in infinity():
		cube = i ** 3
		digit_occurences = get_digit_occurences(cube)
		
		if digit_occurences not in digit_occurences_dict.keys():
			digit_occurences_dict[digit_occurences] = {
											"first_cube": cube,
											"number_of_cubes": 0
										}
		
		digit_occurences_dict[digit_occurences]\
								["number_of_cubes"] += 1
		
		if digit_occurences_dict[digit_occurences]\
									["number_of_cubes"] == 5:
			
			return digit_occurences_dict[digit_occurences]["first_cube"]

if __name__ == "__main__":	
	cProfile.run("print(get_answer())")
